$(function () {
      let userid = getCookie("userid");
      let token = getCookie("token");
      let price_all = 0
      let price_all2 = 0
      let goods_number1 = 0
      let goods_number2 = 0
      


      $(".cart-container").on("click", "a", function () {
            let goods_id = $(this).attr("data-id")
            init2(goods_id)
      })

      $(".cart-container").on("click", ".goods-reduce", function () {
            let goods_nums = $(this).next().val() - 1
            let goods_id = $(this).attr("data-id")
            init3(goods_id, goods_nums)
      })

      $(".cart-container").on("click", ".goods-add", function () {
            let goods_nums = parseInt($(this).prev().val()) + 1
            let goods_id = $(this).attr("data-id")
            init3(goods_id, goods_nums)
      })

      $(".cart-container").on("blur", ".goods-chang", function () {
            let goods_nums = $(this).val()
            let goods_id = $(this).next().attr("data-id")
            init3(goods_id, goods_nums)
      })


      $("#sa").change(function () {
            if ($(this).prop("checked")) {
                  $(".cart-box")
                        .prop(
                              "checked",
                              $("#sa").prop("checked"))
                  price_all = price_all2
                  $("#cart-price-all").html(price_all)
                  init5(1)
                  $("#cart-goods-close2").css({
                        background:"red"
                  })
                  goods_number1 = goods_number2
                  $("#cart-price-all2").html(goods_number1)
                  $("#cart-goods-close2").attr("data",1)
            }
            $("#ua").prop("checked", false)
      });

      $("#ua").change(function () {
            if ($(this).prop("checked")) {
                  $(".cart-box")
                        .prop(
                              "checked",
                              false);
                  price_all = 0
                  $("#cart-price-all").html(price_all)
                  init5(0)
                  $("#cart-goods-close2").css({
                        background:"#bbb"
                  })
                  goods_number1 = 0
                  $("#cart-price-all2").html(goods_number1)
                  $("#cart-goods-close2").attr("data",0)
            }
            $("#sa").prop("checked", false)
      });

      $(".cart-container").on("change", ".cart-box", function () {
            init4($(this).attr("data-id"))
            if ($(this).prop("checked")) {
                  price_all += parseInt($(this).parent().parent().children()[4].innerHTML)
                  $("#cart-price-all2").html(goods_number1)
                  $("#cart-price-all").html(price_all)
                  $("#sa").prop("checked", false)
                  $("#ua").prop("checked", false)
                  $("#cart-goods-close2").css({
                        background:"red"
                  })
                  goods_number1 += parseInt($($(this).parent().parent().children().children()[3]).children()[1].getAttribute("value"))
                  $("#cart-price-all2").html(goods_number1)
                  $("#cart-goods-close2").attr("data",1)
            } else {
                  $("#sa").prop("checked", false)
                  price_all -= parseInt($(this).parent().parent().children()[4].innerHTML)
                  $("#cart-price-all").html(price_all)
                  console.log($(".cart-box"));
                  let q = 0
                  for(let i = 0;i<$(".cart-box").length;i++){
                        if($($(".cart-box")[i]).prop("checked")){
                              q++
                        }    
                  }
                  if(q===0){
                        $("#cart-goods-close2").css({
                              background:"#bbb"
                        })
                        $("#cart-goods-close2").attr("data",0)
                  }
                  goods_number1 -= parseInt($($(this).parent().parent().children().children()[3]).children()[1].getAttribute("value"))
                  $("#cart-price-all2").html(goods_number1)
            }
      })

      $("#cart-goods-close2").on("click",function(){
            if( parseInt($(this).attr("data"))===0){
                  return Tip("请选择商品",0)
            }
            init6()
      })

      function render(res) {
            $(".cart-container").html(
                  res.cart.map(item =>
                        `<div class="goods">
                            <div class="options">
                                  <input type="checkbox" class="cart-box" data-id=${item.goods_id}>
                             </div> 
                            <div class="p-goods">
                                  <div class="p-img">
                                        <img src="${item.img_small_logo}" alt="">
                                  </div>
                                  <div class="p-item">
                                        ${item.title}
                                  </div>
                            </div>
                            <div class="p-price">
                                  ￥${item.price}
                            </div>
                            <div class="p-quantity">
                                  <div class="cartnum">
                                        <button class="goods-reduce" data-id=${item.goods_id}>-</button>
                                        <input class="goods-chang" type="text" value=${item.cart_number}>
                                        <button class="goods-add" data-id=${item.goods_id}>+</button>
                                  </div>
                            </div>
                            <div class="p-sum"> ${item.cart_number * item.price}</div>
                            <div class="p-options">
                                  <a data-id=${item.goods_id} href="javascript:void(0)">删除</a>
                            </div>
                      </div>`
                  ).join("")
            )
      }
      
      function init(){
            var options = {
                  url: "http://127.0.0.1:8888/cart/list",
                  type: "GET",
                  data: {
                        id: userid
                  },
                  headers: {
                        authorization: token
                  },
                  dataType: "json"
            };
            $.ajax(options)
                  .done(function (res) {
                        $("#sa").prop("checked", false)
                        $("#ua").prop("checked", false)
                        render(res)
                        price_all2 = 0
                        price_all = 0
                        goods_number2 = 0
                        res.cart.map(function (item) {
                              price_all2 += parseInt(item.price) * item.cart_number
                              goods_number2 += item.cart_number
                        })
                        console.log(goods_number2);
                        $("#cart-price-all").html(0)
                  })
      }

      function init2(id) {
            var options = {
                  url: "http://127.0.0.1:8888/cart/remove",
                  type: "GET",
                  data: {
                        id: userid,
                        goodsId: id
                  },
                  headers: {
                        authorization: token
                  },
                  dataType: "json"
            };
            $.ajax(options)
                  .done(function () {
                        init()
                  })
      }

      function init3(id, num) {
            var options = {
                  url: `http://127.0.0.1:8888/cart/number?id=${userid}&goodsId=${id}&number=${num}`,
                  type: "POST",
                  data: {
                        id: userid,
                        goodsId: id,
                        number: num
                  },
                  headers: {
                        authorization: token
                  },
                  dataType: "json"
            };
            $.ajax(options)
                  .done(function () {
                        init()
                  })
      }

      function init4(id) {
            var options = {
                  url: `http://127.0.0.1:8888/cart/select?id=${userid}&goodsId=${id}`,
                  type: "POST",
                  data: {
                        id: userid,
                        goodsId: id,
                  },
                  headers: {
                        authorization: token
                  },
                  dataType: "json"
            };
            $.ajax(options)
                  .done(function (res) {
                        console.log(res);
                  })
      }

      function init5(type) {
            var options = {
                  url: `http://127.0.0.1:8888/cart/select/all?id=${userid}&type=${type}`,
                  type: "POST",
                  data: {
                        id: userid,
                        type: type,
                  },
                  headers: {
                        authorization: token
                  },
                  dataType: "json"
            };
            $.ajax(options)
                  .done(function (res) {
                        console.log(res);
                  })
      }

      function init6() {
            var options = {
                  url: `http://127.0.0.1:8888/cart/pay?id=${userid}`,
                  type: "POST",
                  data: {
                        id: userid,
                  },
                  headers: {
                        authorization: token
                  },
                  dataType: "json"
            };
            $.ajax(options)
                  .done(function (res) {
                        switch (res.code) {
                              case 0:
                                  Tip(res.message, res.code)
                                  break;
                              case 1:
                                  Tip(res.message, res.code)
                                  break;
                          }
                        console.log(res);
                  })
      }
      // function init7() {
      //       var options = {
      //             url: `http://127.0.0.1:8888/cart/remove/select`,
      //             type: "GET",
      //             data: {
      //                   id: userid,
      //             },
      //             headers: {
      //                   authorization: token
      //             },
      //             dataType: "json"
      //       };
      //       $.ajax(options)
      //             .done(function (res) {
      //                   switch (res.code) {
      //                         case 0:
      //                             Tip(res.message, res.code)
      //                             break;
      //                         case 1:
      //                             Tip(res.message, res.code)
      //                             break;
      //                     }
      //                     init()
      //                   console.log(res);
      //             })
      // }
      let login_tip = $(".login_tip")
      function Tip(msg, code) {
    if (code === 1) {
        var a = "alert-danger"
        var r = "alert-success"
    } else {
        var a = "alert-success"
        var r = "alert-danger"
    }
    login_tip[0].style.display = "block";
    login_tip[0].classList.remove(a);
    login_tip[0].classList.add(r);
    login_tip[0].innerHTML = msg;
    setTimeout(function () {
        login_tip[0].style.display = "none";
    }, 2000)
      }

      // $(".remove_select button:eq(0)").on("click",function(){
      //       init7()
      // })

      init();
});

